package com.jxj.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.sym.Name;
import com.jxj.entity.User;
import com.jxj.service.UserServiceImpl;

@Controller
public class UserController {
	@Autowired
	private UserServiceImpl serviceImpl;
	@RequestMapping("register")
	public String saveUser(HttpServletRequest request,HttpServletResponse response, User user) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String xuehao = request.getParameter("xuehao");

		if (serviceImpl.findUser(xuehao)) {
			this.serviceImpl.saveuser(user);
			return "/login.jsp";
		}
		else {
			response.getWriter().write("<script> alert('当前学号已被注册');window.location='register.jsp'</script>");
		}
		return null;
		
		
	}

	@RequestMapping("login")
	public String loginUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		String xuehao = request.getParameter("xuehao");		
		String password = request.getParameter("password");	
		String radio=request.getParameter("who");
		User id=this.serviceImpl.findUser1(xuehao);
		
		if (this.serviceImpl.loginuser(xuehao, password)&&radio.equals("用户") ) {
			session.setAttribute("xuehao", xuehao);
			session.setAttribute("radio", radio);
			session.setAttribute("id", id);
			response.getWriter().write("<script> alert('用户登录成功');window.location='main'</script>");
			System.out.println(id+"zxc");
		}
		else if (this.serviceImpl.loginAdmin(xuehao, password)&&radio.equals("管理员")) {
			session.setAttribute("xuehao", xuehao);
			session.setAttribute("radio", radio);
			session.setAttribute("id", id);
			response.getWriter().write("<script> alert('管理员登录成功');window.location='main'</script>");
		}
		else {
			response.getWriter().write("<script> alert('不存在该账户');window.location='login.jsp'</script>");
		}		
		return null;
		
	}
	@RequestMapping("{page}")
	public String demo(@PathVariable String page){
		return "/WEB-INF/"+page+".jsp";
		
	}
	@RequestMapping("help")
	public String findUser(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		String xuehao = (String) session.getAttribute("xuehao");
		User user=this.serviceImpl.findUser1(xuehao);
		session.setAttribute("user", user);		
		request.getRequestDispatcher("help.jsp").forward(request,response);
		return null;		
	}
	@RequestMapping("myMesShow")
	public String myMesShow(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		String xuehao = (String) session.getAttribute("xuehao");
		User user = this.serviceImpl.findUser1(xuehao);
		session.setAttribute("user", user);		
		request.getRequestDispatcher("myMes.jsp").forward(request, response);
		return null;		
	}
	@RequestMapping("updateUser")
	public String updateUser(HttpServletRequest request,HttpServletResponse response,User user)throws IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		String name=request.getParameter("name");
		String email=request.getParameter("email");
		int telephone=Integer.parseInt(request.getParameter("telephone"));
		String school=request.getParameter("school");		
		HttpSession session=request.getSession();
		user=(User) session.getAttribute("user");			
		this.serviceImpl.updateUser(name,telephone,email,user,school);	
		response.getWriter().write("<script> alert('修改成功');window.location='myMesShow'</script>");		
		return null;		
	}
	@RequestMapping("updateMoney")
	public String updateMoney(HttpServletRequest request,HttpServletResponse response)throws IOException{		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");	
		HttpSession session=request.getSession();
		String xuehao = (String) session.getAttribute("xuehao");
		double money =Double.parseDouble(request.getParameter("money"));		
		this.serviceImpl.updateMoney(money,xuehao);

		response.getWriter().write("<script> alert('充值成功');window.location='myMesShow'</script>");		
		return null;
	}
	@RequestMapping("moneyShow")
	public String moneyShow(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{		
		HttpSession session=request.getSession();
		String xuehao = (String) session.getAttribute("xuehao");
		User a=this.serviceImpl.findUser1(xuehao);
		request.setAttribute("a", a);		
		request.getRequestDispatcher("money.jsp").forward(request, response);
		return null;
		
	}
	


}